import { getOptionProps, getAttrs, hasProp, getListeners } from 'ant-design-vue/es/_util/props-util'
import _mergeJSXProps from 'babel-helper-vue-jsx-merge-props'
import _objectWithoutProperties from 'babel-runtime/helpers/objectWithoutProperties'
import _defineProperty from 'babel-runtime/helpers/defineProperty'
import classNames from 'classnames'
const Checkbox = require('ant-design-vue/es/vc-checkbox')

const ZVCheckbox = {}

Object.assign(ZVCheckbox, Checkbox.default, {
  name: 'zvCheckbox',
  data () {
    const checked = hasProp(this, 'checked') ? (this.checked != '0' ? '1' : '0') : (this.defaultChecked != '0' ? '1' : '0')
    return {
      sChecked: checked
    }
  },
  render: function render () {
    const _classNames = {},
      h = arguments[0],
      _getOptionProps = getOptionProps(this),
      prefixCls = _getOptionProps.prefixCls,
      name = _getOptionProps.name,
      id = _getOptionProps.id,
      type = _getOptionProps.type,
      disabled = _getOptionProps.disabled,
      readOnly = _getOptionProps.readOnly,
      tabIndex = _getOptionProps.tabIndex,
      autoFocus = _getOptionProps.autoFocus,
      value = _getOptionProps.value,
      others = _objectWithoutProperties(_getOptionProps, ['prefixCls', 'name', 'id', 'type', 'disabled', 'readOnly', 'tabIndex', 'autoFocus', 'value'])

    const attrs = getAttrs(this)
    const globalProps = Object.keys(Object.assign({}, others, attrs)).reduce(function (prev, key) {
      if (key.substr(0, 5) === 'aria-' || key.substr(0, 5) === 'data-' || key === 'role') {
        prev[key] = others[key]
      }
      return prev
    }, {})

    const sChecked = this.sChecked == '1'

    const classString = classNames(prefixCls, (_classNames, _defineProperty(_classNames, prefixCls + '-checked', sChecked), _defineProperty(_classNames, prefixCls + '-disabled', disabled), _classNames))

    return h(
      'span',
      { class: classString },
      [h('input', _mergeJSXProps([{
        attrs: {
          name: name,
          id: id,
          type: type,
          readOnly: readOnly,
          disabled: disabled,
          tabIndex: tabIndex,

          autoFocus: autoFocus
        },
        class: prefixCls + '-input',
        domProps: {
          checked: !!sChecked,
          value: value
        },
        ref: 'input'
      }, {
        attrs: globalProps,
        on: Object.assign({}, getListeners(this), {
          change: this.handleChange,
          click: this.onClick
        })
      }])), h('span', { class: prefixCls + '-inner' })]
    )
  }
})
Object.assign(ZVCheckbox.props, {
  defaultChecked: {
    type: String
  },
  checked: {
    type: String
  }
})

Object.assign(ZVCheckbox.methods, {
  handleChange (e) {
    const props = getOptionProps(this)
    if (props.disabled) {
      return
    }
    if (!('checked' in props)) {
      this.sChecked = e.target.checked ? '1' : '0'
    }
    this.$forceUpdate() // change前，维持现有状态
    e.shiftKey = this.eventShiftKey
    this.__emit('change', {
      target: Object.assign({}, props, {
        checked: e.target.checked ? '1' : '0'
      }),
      stopPropagation: function stopPropagation () {
        e.stopPropagation()
      },
      preventDefault: function preventDefault () {
        e.preventDefault()
      },
      nativeEvent: e
    })
    this.eventShiftKey = false
    if ('checked' in props) {
      this.$refs.input.checked = props.checked != '0'
    }
  }
})

export default ZVCheckbox
